package com.ruoyi.business.controller;

import com.ruoyi.business.domain.BankCard;
import com.ruoyi.business.domain.vo.BankCardVO;
import com.ruoyi.business.service.BankCardService;
import com.ruoyi.common.annotation.CheckUnique;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TableDataInfo;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 视图接口 : 银行卡
 *
 * @Author : ljq
 * @Created : 2025/10/5
 */
@RestController
@RequestMapping("/business/bankCard")
public class BankCardController extends BaseController {

    @Resource
    private BankCardService service;

    private static final String BASE_AUTH = "business:bankCard";
    private static final String LOG_TITLE = "银行卡#银行卡信息表 -- BankCardController";


    /**
     * 列表
     */
    @PreAuthorize("@ss.hasPermi('" + BASE_AUTH + ":list')")
    @GetMapping("/list")
    public TableDataInfo list(BankCard entity, PageDomain pageDomain) {
        entity.setUserId(SecurityUtils.getUserId());
        return TableDataInfo.build(
                service.listByCondition(entity, pageDomain)
        );
    }


    /**
     * 获取
     */
    @PreAuthorize("@ss.hasPermi('" + BASE_AUTH + ":query')")
    @GetMapping("/{id}")
    public AjaxResult query(@PathVariable Long id) {
        BankCardVO one = new BankCardVO();
        BeanUtils.copyBeanProp(one, service.getById(id));
        return null == one ? AjaxResult.noContent() : AjaxResult.success(one);
    }


    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('" + BASE_AUTH + ":add')")
    @Log(title = LOG_TITLE, businessType = BusinessType.INSERT)
    @CheckUnique(table = "t_bank_card", where = "card_id", message = "银行卡号")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BankCard entity) {
        return toAjax(service.save(entity));
    }


    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('" + BASE_AUTH + ":edit')")
    @Log(title = LOG_TITLE, businessType = BusinessType.UPDATE)
    @CheckUnique(excludeId = "id", table = "t_bank_card", where = "card_id", message = "银行卡号")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BankCard entity) {
        return toAjax(service.updateById(entity));
    }


    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('" + BASE_AUTH + ":remove')")
    @Log(title = LOG_TITLE, businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }


}